package cn.Mmovie.service.impl;

import cn.Mmovie.bean.Auditorium;
import cn.Mmovie.bean.Cinema;
import cn.Mmovie.dao.AuditoriumDao;
import cn.Mmovie.dao.CinemaDao;
import cn.Mmovie.dao.Impl.AuditoriumDaoImpl;
import cn.Mmovie.dao.Impl.CinemaDaoImpl;
import cn.Mmovie.service.AuditoriumService;
import cn.Mmovie.util.PageUtil;

import java.util.List;

public class AuditoriumServiceImpl implements AuditoriumService {
    private AuditoriumDao auditoriumDao=new AuditoriumDaoImpl();
    @Override
    public PageUtil<Auditorium> page(Integer pageNo, Integer pageSize,Integer cinemaId) {
        PageUtil<Auditorium> pageUtil = new PageUtil<>();
        pageUtil.setPageNo(pageNo);
        pageUtil.setPageSize(pageSize);
        pageUtil.setRecordCount(auditoriumDao.selectForRecordTotal());
        pageUtil.setItems(auditoriumDao.selectForPageItems((pageUtil.getPageNo() - 1) * pageUtil.getPageSize(), pageUtil.getPageSize(),cinemaId));
        pageUtil.setUrl("/admin/auditorium_list?pageSize=" + pageUtil.getPageSize());
        return pageUtil;
    }


    @Override
    public Integer deleteAuditoriumById(Integer id) {
        Integer result=-1;
        if (auditoriumDao.selectById(id)!=null){
            result=auditoriumDao.delete(id);
        }
        return result;
    }

    @Override
    public Integer getPageNo(Integer id,Integer pageSize) {
        Integer pageNo=-1;
        if (auditoriumDao.selectById(id)!=null){
            pageNo=auditoriumDao.queryPageNoForAuditoriumId(id,pageSize);
        }
       return pageNo;
    }

    @Override
    public Integer addAuditorium(Auditorium auditorium) {
        Integer res=-1;
        if (auditoriumDao.selectById(auditorium.getId())==null&&auditoriumDao.selectById(auditorium.getId()).getName()==null) {
            res=auditoriumDao.insert(auditorium);
        }
        return res;
    }

    @Override
    public Auditorium queryById(Integer id) {
        return auditoriumDao.selectById(id);
    }

    @Override
    public Integer modifyAuditorium(Auditorium auditorium) {
        Integer res=-1;
        if (auditoriumDao.selectById(auditorium.getId())!=null&&auditoriumDao.selectAuditoriumByName(auditorium.getName())==null) {
            res=auditoriumDao.update(auditorium);
        }
        return res;
    }

    @Override
    public PageUtil<Auditorium> page(Integer pageNo, Integer pageSize) {
        PageUtil<Auditorium> pageUtil = new PageUtil<>();
        pageUtil.setPageNo(pageNo);
        pageUtil.setPageSize(pageSize);
        pageUtil.setRecordCount(auditoriumDao.selectForRecordTotal());
        pageUtil.setItems(auditoriumDao.selectForPageItems((pageUtil.getPageNo() - 1) * pageUtil.getPageSize(), pageUtil.getPageSize()));
        pageUtil.setUrl("/admin/cinema_list?pageSize=" + pageUtil.getPageSize());
        return pageUtil;
    }

    @Override
    public List<Auditorium> queryAllAuditorium() {
        return auditoriumDao.selectAll();
    }
}
